<?php
#!/bin/bash

declare(strict_types=1);

/**
 * User: hzbskak
 * Date: 2020/1/17 - 11:03
 */

namespace app\pub\model;


use think\Model;

/**
 * 公共模型缓存类
 * @date 2020年1月17日11:03:37
 * Class SearchModel
 * @package app\pub\model
 */
class SearchModel extends Model
{

    public $searchListNum = 8; //搜索列表的长度

    public function structureSearchCacheData( $data, $search_type = 'all', $lang_type = 'zh_hk')
    {
        $return_data = [];
        switch ( $search_type)
        {
            case 'area' :
                $return_data['area'] = $this->structureSearchAreaCacheData(
                    $data['area'], $lang_type, $this->searchListNum); // 只搜索地区缓存数据
                break;
            default :
                $return_data = $this->structureSearchAllCacheData( $data, $lang_type);
                break;
        }
        return $return_data;
    }

    public function structureSearchAreaCacheData( $data, $lang_type = 'zh_hk', $num = 99999999)
    {
        $arr = $this->srtuctreArea( $data, $lang_type);
        return $arr;
    }

    public function srtuctreArea( $data, $lang_type = 'zh_hk', $num = 99999999)
    {
        $arr = [ 'country' => [], 'city' => []];

        $c_type_pattern = "/CTYPE_(.*?)&/"; // 获取地区类型的正则

        $i = 0;
        if( !empty( $data))
        {
            foreach ( $data as $key => $value)
            {
                preg_match_all( $c_type_pattern, $key, $c_type); // 获取数据类型

                foreach ( $value as $k => $v)
                {
                    $v[$lang_type]['newName'] = strstr( $v[$lang_type]['name'], ' · ', true);
                    if( !$v[$lang_type]['newName'])
                    {
                        $v[$lang_type]['newName'] = $v[$lang_type]['name'];
                        $v[$lang_type]['allName'] = $v[$lang_type]['name'];
                        $v[$lang_type]['EnewName'] = $v['en_us']['name']; // 获取英文名称
                        $v[$lang_type]['EallName'] = $v['en_us']['name']; // 获取英文名称
                    }
                    else
                    {
                        $v[$lang_type]['allName'] =
                            trim( strstr( $v[$lang_type]['name'], ' · '), ' · ');
                        $v[$lang_type]['EnewName'] =
                            strstr( $v['en_us']['name'], ' · ', true); // 获取英文名称
                        $v[$lang_type]['EallName'] =
                            trim( strstr( $v['en_us']['name'], ' · '), ' · '); // 获取英文名称
                    }

                    if( $c_type[0][1] == 1)
                    {
                        $arr['country'][$key] = $v[$lang_type]; // 获取对应的缓存数据
                    }
                    else
                    {
                        if( $num > 0)
                        {
                            $arr['city'][$key] = $v[$lang_type]; // 获取对应的语言数据
                            $num--;
                        }
                    }
                    $i++;
                }
            }
        }

        return $arr;
    }




















}